//
//  AppDelegate+Init.m
//  StaffTax
//
//  Created by 侯伟 on 2020/4/20.
//  Copyright © 2020 侯伟. All rights reserved.
//

#import "AppDelegate+Init.h"
#import "QXAdvertManager.h"
#import "IdfaTool.h"
#import <IQKeyboardManager/IQKeyboardManager.h>
#import <SDWebImage/SDWebImageDownloader.h>
#import <SDWebImage/SDWebImageManager.h>
#import <UMCommon/UMCommon.h>
#import <SJMediaCacheServer/SJMediaCacheServer.h>
#import <SJVideoPlayer/SJVideoPlayer.h>
#import <SJVideoPlayer/SJVideoPlayerConfigurations.h>

#import "RootNavigationViewController.h"

@implementation AppDelegate (Other)

- (void)configSDKs:(NSDictionary *)launchOptions{
    
    // 广告
    [self configAd];
    
    //友盟
    [self configUM:launchOptions];
    
    //键盘
    [self configKeyBoard];
    
    //web
    [self configSDWebImage];
    
    /// 缓存
    [self configCache];
    
    
    [self configPlayer];
}

/// 广告
- (void)configAd{
    
    [IdfaTool requestIDFA];
    
    [[QXAdvertManager manager] initSDKWithChannel:ADChannelSElF appId:AD_APPID_SELF sAdId:AD_ID_SPLASH_SELF vAdId:AD_ID_VIDEO_SELF bAdid:AD_ID_BANNER_SELF];
    [[QXAdvertManager manager] initSDKWithChannel:ADChannelCSJ appId:AD_APPID_CSJ sAdId:AD_ID_SPLASH_CSJ vAdId:AD_ID_VIDEO_CSJ bAdid:AD_ID_BANNER_CSJ];
    [[QXAdvertManager manager] initSDKWithChannel:ADChannelYLH appId:AD_APPID_YLH sAdId:AD_ID_SPLASH_YLH vAdId:AD_ID_VIDEO_YLH bAdid:AD_ID_BANNER_YLH];
}

/// 键盘
- (void)configKeyBoard{
    IQKeyboardManager* keyBoard = [IQKeyboardManager sharedManager];
    keyBoard.enable = YES;
    keyBoard.keyboardDistanceFromTextField = PX(16);
    keyBoard.shouldToolbarUsesTextFieldTintColor = NO;
    keyBoard.toolbarTintColor = COLOR_THEME;
    keyBoard.placeholderFont = FONT(14);
    keyBoard.placeholderColor = COLOR_GRAY_BASE;
    keyBoard.shouldResignOnTouchOutside = YES;
    keyBoard.enableAutoToolbar = YES;
}

/// 友盟
- (void)configUM:(NSDictionary *)launchOptions{
    
#ifdef DEBUG
    [UMConfigure setLogEnabled:YES];
#endif
    [UMConfigure initWithAppkey:KEY_UM channel:@"App Store"];
}

/// 图片缓存
- (void)configSDWebImage{
    [[SDWebImageManager sharedManager] removeAllFailedURLs];
}

/// 视频缓存
- (void)configCache{
    SJMediaCacheServer.shared.maxConcurrentExportCount = 5;
    SJMediaCacheServer.shared.cacheCountLimit = 10;
    SJMediaCacheServer.shared.maxDiskAgeForCache = 7*24*60*60; // 保存时长限制
    SJMediaCacheServer.shared.maxDiskSizeForCache = 5*1024*1024*1024; // 缓存占用的磁盘空间限制
}

- (void)configPlayer{
    SJVideoPlayer.updateResources(^(id<SJVideoPlayerControlLayerResources>  _Nonnull resources) {
        resources.progressThumbImage = [UIImage imageNamed:@"details_play_progress"];
        resources.progressThumbColor = UIColor.whiteColor;
        resources.backImage = [UIImage imageNamed:@"common_back_white"];
    });
}

#pragma mark -- 分享回调

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options{
    return YES;
}

- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void(^)(NSArray * __nullable restorableObjects))restorationHandler{
    return YES;
}

@end
